﻿using System.Web;
using System.Web.UI;
using System.Web.Hosting;

namespace N2.Workflow
{
	public static class ClientScriptManagerProvider
	{
		static ClientScriptManager s_cs;
		
		public static ClientScriptManager ClientScript {
			get {
				if (!HostingEnvironment.IsHosted) {
					return null;
				}

				if (null == s_cs) {
					Page _page = HttpContext.Current.Handler as Page;
					//HACK research an implication of enforcing a page lifecycle manually on performance and stability
					if (null == _page) {
						_page = new Page();
						((IHttpHandler)_page).ProcessRequest(HttpContext.Current);
					}

					s_cs = _page.ClientScript;
				}
				return s_cs;
			}
		}
	}
}
